package com.bingyuan.aipassproject.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bingyuan.aipassproject.AndroidApplication;
import com.bingyuan.aipassproject.di.component.ApplicationComponent;
import com.bingyuan.aipassproject.di.modules.ActivityModule;
import com.bingyuan.aipassproject.view.BaseActivity;

import java.util.Objects;

public abstract class BaseFragment extends Fragment{

    protected Context mContext;
    private View rootView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        rootView = LayoutInflater.from(mContext).inflate(getLayoutResId(), null);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initializeInjector();
        initView(view);
        onClick();
    }

    protected ApplicationComponent getApplicationComponent() {
        return ((AndroidApplication) Objects.requireNonNull(getActivity()).getApplication())
                .getApplicationComponent();
    }

    /**
     * Get an Activity module for dependency injection.
     */
    protected ActivityModule getActivityModule() {
        return new ActivityModule((BaseActivity) getActivity());
    }

    protected abstract int getLayoutResId();

    protected abstract void onClick();

    protected void initView(View view) {

    }

    protected void initializeInjector() {

    }
}
